package com.cl.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;


import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 云之家组织与人员同步API接口
 * @author chenlu
 * @date 2018-3-14 上午10:08:25
 */
public class OpenaccessUtils {
	private static Logger logger = LoggerFactory.getLogger(OpenaccessUtils.class);
	
	/**
	 * 查询全部组织信息（不包括商务伙伴部门）
	 * @return json
	 * @throws Exception 
	 */
	public static JSONArray deptGetall() throws Exception{
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String eid = rb.getString("eid");
		String wxUrl = rb.getString("wxUrl");
		String url = wxUrl + "openaccess/input/dept/getall";
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();  
        nvps.add(new BasicNameValuePair("nonce", String.valueOf(new Date().getTime())));  
        nvps.add(new BasicNameValuePair("eid", eid));  
        JSONObject json = new JSONObject();
        json.put("eid", eid);
        nvps.add(new BasicNameValuePair("data", RSAUtils.enyte(json.toString())));  
		String reponse = YZJHttpUtils.sendPost(url,nvps);
		JSONObject obj = JSONObject.parseObject(reponse);
		if(obj.getBooleanValue("success")){
			return obj.getJSONArray("data");
		}else{
			logger.error("[deptGetall"+obj.getString("errorCode")+"]："+obj.getString("error"));
		}
		return new JSONArray();
	}
	
	/**
	 * 查询全部人员信息（不包括商务伙伴）
	 * @param begin
	 * @param count
	 * @return
	 * @throws Exception 
	 */
	public static JSONArray personGetall(int begin, int count) throws Exception{
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String eid = rb.getString("eid");
		String wxUrl = rb.getString("wxUrl");
		String url = wxUrl + "openaccess/input/person/getall";
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();  
        nvps.add(new BasicNameValuePair("nonce", String.valueOf(new Date().getTime())));  
        nvps.add(new BasicNameValuePair("eid", eid));  
        JSONObject json = new JSONObject();
        json.put("eid", eid);
        json.put("begin", begin);
        json.put("count", count);
        nvps.add(new BasicNameValuePair("data", RSAUtils.enyte(json.toString())));  
		String reponse = YZJHttpUtils.sendPost(url,nvps);
		JSONObject obj = JSONObject.parseObject(reponse);
		if(obj.getBooleanValue("success")){
			return obj.getJSONArray("data");
		}else{
			logger.error("[personGetall"+obj.getString("errorCode")+"]："+obj.getString("error"));
		}
		return new JSONArray();
	}
	
	/**
	 * 获取内部所有兼职信息
	 * @param begin
	 * @param count
	 * @return
	 * @throws Exception 
	 */
	public static JSONArray personGetallcasvir () throws Exception{
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String eid = rb.getString("eid");
		String wxUrl = rb.getString("wxUrl");
		String url = wxUrl + "openaccess/input/person/getallcasvir";
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();  
        nvps.add(new BasicNameValuePair("nonce", String.valueOf(new Date().getTime())));  
        nvps.add(new BasicNameValuePair("eid", eid));  
        JSONObject json = new JSONObject();
        json.put("eid", eid);
        nvps.add(new BasicNameValuePair("data", RSAUtils.enyte(json.toString())));  
		String reponse = YZJHttpUtils.sendPost(url,nvps);
		JSONObject obj = JSONObject.parseObject(reponse);
		if(obj.getBooleanValue("success")){
			return obj.getJSONArray("data");
		}else{
			logger.error("[personGetallcasvir"+obj.getString("errorCode")+"]："+obj.getString("error"));
		}
		return new JSONArray();
	}
	
	/**
	 * 添加内部兼职人员信息的接口
	 * @param begin
	 * @param count
	 * @return
	 * @throws Exception 
	 */
	public static JSONArray personAddcasvir(JSONArray userArr) throws Exception{
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String eid = rb.getString("eid");
		String wxUrl = rb.getString("wxUrl");
		String url = wxUrl + "openaccess/input/person/addcasvir";
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();  
        nvps.add(new BasicNameValuePair("nonce", String.valueOf(new Date().getTime())));  
        nvps.add(new BasicNameValuePair("eid", eid));  
        JSONObject json = new JSONObject();
        json.put("eid", eid);
        json.put("type", 1);
        json.put("list", userArr);
        nvps.add(new BasicNameValuePair("data", RSAUtils.enyte(json.toString())));  
		String reponse = YZJHttpUtils.sendPost(url,nvps);
		JSONObject obj = JSONObject.parseObject(reponse);
		if(obj.getBooleanValue("success")){
			return obj.getJSONArray("data");
		}else{
			logger.error("[personAddcasvir"+obj.getString("errorCode")+"]："+obj.getString("error"));
		}
		return new JSONArray();
	}
	
	/**
	 * 获取最近更新组织 （7.0.5+版本支持）
	 * @param time
	 * @return
	 * @throws Exception 
	 */
	public static JSONArray deptGetAtTime(String time) throws Exception{
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String eid = rb.getString("eid");
		String wxUrl = rb.getString("wxUrl");
		String url = wxUrl + "openaccess/input/dept/getAtTime";
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();  
        nvps.add(new BasicNameValuePair("nonce", String.valueOf(new Date().getTime())));  
        nvps.add(new BasicNameValuePair("eid", eid));  
        JSONObject json = new JSONObject();
        json.put("eid", eid);
        json.put("time", time);
        nvps.add(new BasicNameValuePair("data", RSAUtils.enyte(json.toString())));  
		String reponse = YZJHttpUtils.sendPost(url,nvps);
		JSONObject obj = JSONObject.parseObject(reponse);
		if(obj.getBooleanValue("success")){
			return obj.getJSONArray("data");
		}else{
			logger.error("[deptGetAtTime"+obj.getString("errorCode")+"]："+obj.getString("error"));
		}
		return new JSONArray();
	}
	
	/**
	 * 查询已更新人员信息
	 * @param time
	 * @param begin
	 * @param count
	 * @return
	 * @throws Exception 
	 */
	public static JSONArray personGetAtTime(String time,int begin, int count) throws Exception{
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String eid = rb.getString("eid");
		String wxUrl = rb.getString("wxUrl");
		String url = wxUrl + "openaccess/input/person/getAtTime";
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();  
        nvps.add(new BasicNameValuePair("nonce", String.valueOf(new Date().getTime())));  
        nvps.add(new BasicNameValuePair("eid", eid));  
        JSONObject json = new JSONObject();
        json.put("eid", eid);
        json.put("time", time);
        json.put("begin", begin);
        json.put("count", count);
        nvps.add(new BasicNameValuePair("data", RSAUtils.enyte(json.toString())));  
		String reponse = YZJHttpUtils.sendPost(url,nvps);
		JSONObject obj = JSONObject.parseObject(reponse);
		if(obj.getBooleanValue("success")){
			return obj.getJSONArray("data");
		}else{
			logger.error("[personGetAtTime"+obj.getString("errorCode")+"]："+obj.getString("error"));
		}
		return new JSONArray();
	}
	
	/**
	 * 添加内部兼职人员信息的接口
	 * @param begin
	 * @param count
	 * @return
	 * @throws Exception 
	 */
	public static JSONArray personUpdatecasvir(JSONArray userArr) throws Exception{
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String eid = rb.getString("eid");
		String wxUrl = rb.getString("wxUrl");
		String url = wxUrl + "openaccess/input/person/updatecasvir";
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();  
        nvps.add(new BasicNameValuePair("nonce", String.valueOf(new Date().getTime())));  
        nvps.add(new BasicNameValuePair("eid", eid));  
        JSONObject json = new JSONObject();
        json.put("eid", eid);
        json.put("type", 1);
        json.put("list", userArr);
        nvps.add(new BasicNameValuePair("data", RSAUtils.enyte(json.toString())));  
		String reponse = YZJHttpUtils.sendPost(url,nvps);
		JSONObject obj = JSONObject.parseObject(reponse);
		if(obj.getBooleanValue("success")){
			return obj.getJSONArray("data");
		}else{
			logger.error("[personUpdatecasvir"+obj.getString("errorCode")+"]："+obj.getString("error"));
		}
		return new JSONArray();
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(OpenaccessUtils.deptGetAtTime("2017-05-17 18:55:00").toJSONString());
	}
	
}
